package com.itheima.reggie.exception;


import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;
@Slf4j
@RestControllerAdvice
public class GoalExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> handlerException(SQLIntegrityConstraintViolationException e) {
        //e.printStackTrace();
        String message = e.getMessage();//Duplicate entry '666' for key 'idx_username'
        String[] s = message.split(" ");
        return R.error(s[2].replaceAll("'","")+"已处在");
    }


    /*
    * 异常处理方法
    * */
    @ExceptionHandler(CustomException.class)
    public R<String> handlerRuntimeException(CustomException e) {
        //e.printStackTrace();
        log.error(e.getMessage());
        return R.error(e.getMessage());
    }


}
